package com.dudu.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dudu.bean.ImsLogBean;
import com.dudu.bean.InterfaceLogBean;
import com.dudu.bean.SystemLogBean;
import com.dudu.bean.UserBean;
import com.dudu.service.ILogService;
import com.dudu.service.impl.LogService;

import core.CoreService;
/**
 * 日志
 * @author gaoxs
 *
 */
public class SystemLogController extends CoreService implements ISystemLogController {
	//当前登陆用户
	private  final String KEY_CURRENT_USER = "key_current_user";
	//用于记录相应接口调用的日志
	private ILogService logService = new LogService();	
	/**
	 * 系统日志
	 * @return
	 */
	public String queryAll() {
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		// 判断如果用户信息不为空则继续下一步
		if(currentUser != null){
	        JSONArray jsonArray = new JSONArray();
	        //获取分页信息
	        String pageSql = this.getPageSql();
	        //获取查询条件
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = logService.getSysLogTotalAcount(paramSql);
	        //查询日志
	        List<SystemLogBean> logs =logService.querySysLogAll( paramSql, pageSql);
	        //转成JSON
	        jsonArray = JSONArray.fromObject(logs);
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);		
		} else {
			object.put("flag", "100000");
		}
			
		return returnData( object.toString() );
    }

	/**
	 * 第三方能力平台调用日志
	 * @return
	 */
	public String queryImsLog() {
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		// 判断如果用户信息不为空则继续下一步
		if(currentUser != null){
			JSONArray jsonArray = new JSONArray();
			//获取分页信息
	        String pageSql = this.getPageSql();
	        //获取查询条件
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = logService.getImsLogTotalAcount(paramSql);
	        ////查询日志
	        List<ImsLogBean> logs = logService.queryImsLog( paramSql, pageSql);
	        //转成JSON
	        jsonArray = JSONArray.fromObject(logs);
	        //设置返回值
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
		} else {
			object.put("flag", "100000");
		}
			
		return returnData( object.toString() );
	}
	
	/**
	 * 能力平台日志
	 * @return
	 */
	public String queryInterfaceLog() {
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		// 判断如果用户信息不为空则继续下一步
		if(currentUser != null){
			JSONArray jsonArray = new JSONArray();
			//获取分页信息
	        String pageSql = this.getPageSql();
	        //获取查询条件
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = logService.getInterfaceLogTotalAcount(paramSql);
	        ////查询日志
	        List<InterfaceLogBean> logs = logService.queryInterfaceLog( paramSql, pageSql);
	        //转成JSON
	        jsonArray = JSONArray.fromObject(logs);
	        //设置返回值
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
		} else {
			object.put("flag", "100000");
		}
			
		return returnData( object.toString() );
	}
}
